package com.jf.yytgd.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA
 * Description: 对象工具类
 * User:22954
 * Date:2025-01-25
 * Time:18:47
 */
@Slf4j
public class ObjectUtil {
    public static <T> T clone(Object o, Class<T> cls, boolean nullNew) {
        try {
            Constructor<?> constructor = cls.getDeclaredConstructor();
            Object object = constructor.newInstance();
            if (o == null) {
                return nullNew ? (T) constructor.newInstance() : null;
            }
            Field[] fields = cls.getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);
                field.set(object, field.get(o));
            }
            return (T) object;
        } catch (NoSuchMethodException | InvocationTargetException | InstantiationException |
                 IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    public static <T> T clone(Object o, Class<T> cls) {
        return clone(o, cls, false);
    }

    public static Map<String, Object> nonEmptyFieldMap(Object o) {
        Map<String, Object> map = new HashMap<>();
        Field[] fields = o.getClass().getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            try {
                Object value = field.get(o);
                String name = convertToSnakeCase(field.getName());
                if (value != null) {
                    if (value instanceof String) {
                        if (!((String) value).isEmpty()) {
                            map.put(name, value);
                        }
                    } else {
                        map.put(name, value);
                    }
                }
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }
        return map;
    }

    public static String convertToSnakeCase(String str) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (Character.isUpperCase(c)) {
                sb.append("_").append(Character.toLowerCase(c));
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }
    public static Map<String, Object> parseJson(String json) {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            return objectMapper.readValue(json, new TypeReference<Map<String, Object>>() {});
        } catch (JsonProcessingException e) {
            log.error("Failed to parse JSON: " + json, e);
        }
        return null;
    }
    public static String toJson(Object obj) {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            return objectMapper.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            log.error("Failed to transfer to JSON: " + obj, e);
        }
        return null;
    }
}
